<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box-card">
      <el-button type="primary" @click="gosave()">添加商品分类</el-button>
      <zk-table
        class="tabletop"
        :data="tableData"
        :columns="columns"
        :selection-type="false"
        :expand-type="false"
        show-index
        index-text="#"
        border
        :show-row-hover="false"
      >
        <template slot="isvalid" slot-scope="scope">
          <i
            class="el-icon-success"
            style="color: lightgreen"
            v-if="scope.row.cat_deleted === false"
          ></i>
          <i class="el-icon-error" style="color: red" v-else></i>
        </template>

        <template slot="order" slot-scope="scope">
          <el-tag v-show="scope.row.cat_level === 0">一级</el-tag>
          <el-tag type="success" v-show="scope.row.cat_level === 1"
            >二级</el-tag
          >
          <el-tag type="info" v-show="scope.row.cat_level === 2">三级</el-tag>
        </template>

        <template slot="handle" slot-scope="scope">
          <el-button @click="goeait(scope.row)" type="primary" size="mini"
            >编辑</el-button
          >
          <el-button type="danger" size="mini" @click="del(scope.row)"
            >删除</el-button
          >
        </template>
      </zk-table>

      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryinfo.pagesize"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="5"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totalsize"
      ></el-pagination>

      <el-dialog
        title="添加分类"
        :visible.sync="isShow"
        :close-on-click-modal="false"
        @close="clearform"
      >
        <div slot="footer" class="dialog-footer">
          <el-button @click="isShow = false">取 消</el-button>
          <el-button type="primary" @click="save()">确 定</el-button>
        </div>

        <el-form
          :model="saveobj"
          :rules="checkform"
          ref="ruleForm"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="分类名称:" prop="cat_name">
            <el-input v-model="saveobj.cat_name"></el-input>
          </el-form-item>
          <el-form-item label="父级分类" prop="cat_pid">
            <el-cascader
              v-model="selectkeys"
              :options="options"
              :props="{
                expandTrigger: 'hover',
                label: 'cat_name',
                value: 'cat_id',
              }"
              clearable
              @change="handleChange"
            ></el-cascader>
          </el-form-item>
        </el-form>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
export default {
  name: "goodstype",
  data() {
    return {
      isShow: false,
      queryinfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5,
      },
      totalsize: 0,
      tableData: [],
      columns: [
        {
          label: "分类名称",
          prop: "cat_name",
        },
        {
          label: "是否有效",
          type: "template",
          template: "isvalid",
        },
        {
          label: "分类级别",
          type: "template",
          template: "order",
        },
        {
          label: "操作",
          type: "template",
          template: "handle",
          width: 180,
        },
      ],
      selectkeys: [],
      options: [],
      saveobj: {
        cat_pid: 0,
        cat_name: "",
        cat_level: 0,
        cat_id: 0,
        attrId: 0,
      },
      checkform: {
        cat_name: [
          { required: true, message: "请输入分类名称", trigger: "blur" },
        ],
      },
    }
  },
  methods: {
    async query() {
      let rst = await this.$http.get("categories", this.queryinfo);
      console.log(rst);
      if (rst.meta.status == 200) {
        this.tableData = rst.data.result;
        this.totalsize = rst.data.total;
      } else {
        this.$message.error("获取商品分类列表失败");
      }
    },
    handleSizeChange(size) {
      this.queryinfo.pagesize = size;
      this.query();
    },
    handleCurrentChange(pagenum) {
      this.queryinfo.pagenum = pagenum;
      this.query();
    },
    gosave() {
      this.isShow = true;
    },
    async gettypes() {
      let rst = await this.$http.get("categories", { type: 2 });
      if (rst.meta.status == 200) {
        this.options = rst.data;
      } else {
        this.$message.error("获取父级分类失败");
      }
      console.log(rst);
    },
    handleChange() {
      if (this.selectkeys.length > 0) {
        this.saveobj.cat_pid = this.selectkeys[this.selectkeys.length - 1];
        this.saveobj.cat_level = this.selectkeys.length;
      } else {
        this.saveobj.cat_pid = 0;
        this.saveobj.cat_level = 0;
      }
    },
    clearform() {
      this.saveobj = {
        cat_pid: 0,
        cat_name: "",
        cat_level: 0,
        cat_id: 0,
      }
      this.selectkeys = [];
    },
    save() {
      let rst = null;
      this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          if (this.saveobj.cat_id == 0) {
            rst = await this.$http.post("categories", this.saveobj);
          } else {tr
            rst = await this.$http.put(
              `categories/${this.saveobj.cat_id}`,
              this.saveobj
            );
          }
          if (rst.meta.status == 201 || rst.meta.status == 200) {
            this.query();
            this.isShow = false;
          } else {
            this.$message.error("保存失败");
          }
        } else {
          this.$message.error("请按照要求完善输入信息");
          return false;
        }
      });
    },
    goeait(row) {
      this.saveobj.cat_level = row.cat_level;
      this.saveobj.cat_id = row.cat_id;
      this.saveobj.cat_pid = row.cat_pid;
      this.saveobj.cat_name = row.cat_name;
      this.isShow = true;
    },
    del(row) {
      this.$confirm(`此操作将永久删除该文件，是否继续？`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          let rst = await this.$http.delete(`categories/${row.cat_id}`);
          console.log(rst);
          if (rst.meta.status == 200) {
            this.query();
          } else {
            this.$message.error("删除失败");
          }
        })
        .catch(() => {
          this.$message.info("已取消删除");
        });
    },
  },
  mounted() {
    this.query();
    this.gettypes();
  },
};
</script>

<style>
.box-card {
  margin-top: 20px;
}
</style>